<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>计算随机数平均值</title>
	</head>
	<body>
			<body>
		<input type="button"value="计算"id="agg" />
		<input type="button" value="平均值"id="agg2" />
		<p id="num"></p>
		<p id="avg"></p>
		<script type="text/javascript">
		var a=[];		
		var agg=document.getElementById("agg");
		var agg2=document.getElementById("agg2");
			 agg.onclick=function (){
			 	var p=document.getElementById("num");
			 var r=parseInt(Math.random()*100);//取随机数
			 a.push(r)
			 var span=document.createElement("span");
			 span.innerHTML=r;
			 span.style.marginLeft="20px";
			 p.appendChild(span);
				}
			 agg2.onclick=function(){
			 	var s=0;
			 	for(var i=0;i<a.length;i++ ){
			 		s=s+a[i];
			 	}
				document.getElementById("avg").innerHTML=s/a.length;
			}
		</script>
	</body>
</html>
